package com.freejy.ecommunity.common.pagination;

import java.io.Serializable;
import java.util.List;

/**
 * Page object.
 * @author Jerry
 * @param <T> the generic type
 */
@SuppressWarnings("serial")
public class Page<T> implements Serializable{
	
	/** The page no. */
	private int pageNo;
	
	/** The page size. */
	private int pageSize;
	
    /** The total rows. */
    private int totalCount;
    
    /** The total pages. */
    private int totalPages;
    
    /** The result list. */
    private List<T> resultList;

    
    public Page()
	{
		super();
	}

	public Page(int pageNo, int pageSize)
	{
		super();
		this.pageNo = pageNo;
		this.pageSize = pageSize;
	}

	public Page(int pageNo, int pageSize, int totalCount, List<T> resultList)
	{
		super();
		this.totalCount = totalCount;
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.resultList = resultList;
	}

    /**
     * Gets the result list.
     *
     * @return the result list
     */
    public List<T> getResultList() {
        return resultList;
    }

    /**
     * Sets the result list.
     *
     * @param resultList the new result list
     */
    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }

    /**
     * Gets the total pages.
     *
     * @return the total pages
     */
    public int getTotalPages() {
        if (totalCount % pageSize == 0) {
            totalPages = totalCount / pageSize;
        } else {
            totalPages = (totalCount / pageSize) + 1;
        }
        return totalPages;
    }

    /**
     * Gets the total rows.
     *
     * @return the total rows
     */
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * Sets the total rows.
     *
     * @param totalRows the new total rows
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * Gets the offset.
     *
     * @return the offset
     */
    public int getOffset() {
        return (pageNo - 1) * pageSize;
    }

    /**
     * Gets the end index.
     *
     * @return the end index
     */
    public int getEndIndex() {
        if (getOffset() + pageSize > totalCount) {
            return totalCount;
        } else {
            return getOffset() + pageSize;
        }
    }

    /**
     * Gets the page no.
     *
     * @return the page no
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * Sets the page no.
     *
     * @param pageNo the new page no
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Page [totalCount=");
		builder.append(totalCount);
		builder.append(", totalPages=");
		builder.append(totalPages);
		builder.append(", pageNo=");
		builder.append(pageNo);
		builder.append(", pageSize=");
		builder.append(pageSize);
		builder.append(", resultList=");
		builder.append(resultList);
		builder.append("]");
		return builder.toString();
	}
    
    
}
